using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using Microsoft.ACEEngineering.Utility;
using Microsoft.AceEngineering.SPM.SPMBusinessLayer;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;

public partial class Controls_SPMSearch : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAttributes();
            GetScores();
            GetOrgNameList();
            gridAppList.Visible = false;
            ViewState["exprAndDir"] = string.Empty;

            AdvSearchCDE sData = (AdvSearchCDE)Session["search"];
            if (sData != null && Session["fView"]!=null)
            {
                Session["search"] = null;
                for (int i = 0; i < lstAttributes.Items.Count; i++)
                {
                    if (sData.Attributes.Contains(lstAttributes.Items[i].Value))
                    {
                        lstAttributes.Items[i].Selected = true;
                    }
                }

                txtAppName.Text = sData.ApplicatioName;
                drpThreshold.SelectedValue = sData.Score.ToString();
                drpOrganization.SelectedValue = sData.Organization.ToString();

                string enabledAttributes = Microsoft.ACEEngineering.Utility.CommonProcess.GetCommaSeparatedValuesofSelectedItems(lstAttributes);
                ViewState["enabledatt"] = enabledAttributes.Trim();
                ViewState["app"] = txtAppName.Text;
                ViewState["threshold"] = drpThreshold.SelectedValue;
                ViewState["organization"] = drpOrganization.SelectedValue;
                ViewState["exprAndDir"] = sData.SortExpr;

                gridAppList.Visible = true;
                gridAppList.PageIndex = sData.Page;
                gridAppList.DataBind();

                if (gridAppList.Rows.Count == 0 && gridAppList.PageCount >= 1)
                {
                    gridAppList.DataBind();
                }
            }
            Session["fView"] = null;
            Session["search"] = null;
        }
    }

    private void GetOrgNameList()
    {
        ArrayList organizations = SearchBL.GetOrganizationsDDL();
        foreach (ListItem lstTemp in organizations)
        {
            drpOrganization.Items.Add(lstTemp);
        }
        drpOrganization.SelectedIndex = 0;
        drpOrganization.DataBind();
    }

    private void GetAttributes()
    {
        ArrayList attributes = SearchBL.GetAttributes();
        foreach (ListItem lstTemp in attributes)
        {
            lstAttributes.Items.Add(lstTemp);
        }
        lstAttributes.SelectedIndex = -1;
        lstAttributes.DataBind();
    }

    private void GetScores()
    {
        ArrayList attributes = null;
        attributes = SearchBL.GetThresholdScores();

        foreach (ListItem lstTemp in attributes)
        {
            drpThreshold.Items.Add(lstTemp);
        }

        drpThreshold.SelectedIndex = 0;
        drpThreshold.DataBind();

        if (drpThreshold.Items.Count != 1)
        {
            string strTopValue = drpThreshold.Items[1].Text;
            drpThreshold.Items[1].Text = strTopValue + " (or greater)";
        }
    }

    protected void obdsAppList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (string.IsNullOrEmpty(ViewState["enabledatt"].ToString()))
            e.InputParameters[0] = null;
        else
            e.InputParameters[0] = ViewState["enabledatt"].ToString();
        if (ViewState["app"].ToString().Length > 0)
            e.InputParameters[1] = ViewState["app"].ToString();
        else
            e.InputParameters[1] = null;
        e.InputParameters[2] = Int32.Parse(ViewState["threshold"].ToString());
        e.InputParameters[3] = Int32.Parse(ViewState["organization"].ToString());
        e.InputParameters[4] = ViewState["exprAndDir"].ToString();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["enabledatt"] = Microsoft.ACEEngineering.Utility.CommonProcess.GetCommaSeparatedValuesofSelectedItems(lstAttributes);
        ViewState["app"] = txtAppName.Text;
        ViewState["threshold"] = drpThreshold.SelectedValue;
        ViewState["organization"] = drpOrganization.SelectedValue;
        gridAppList.Visible = true;
        gridAppList.PageIndex = 0;
        gridAppList.DataBind();
    }

    public void gridAppList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strStartDate = e.Row.Cells[4].Text;
            e.Row.Cells[4].Text = strStartDate.Replace(" 12:00AM", "");
        }
    }

    public void gridAppList_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpr = null;
        string dir = string.Empty;

        if(e.SortDirection.ToString().Contains("Desc"))
            dir = "DESC";

        sortExpr = e.SortExpression + " " + dir;

        ViewState["exprAndDir"] = sortExpr.Trim();
    }

    protected void obdsASearch_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue.GetType().ToString() == "System.Int32")
        {
            int totalRecords = (int)e.ReturnValue;
            gridAppList.Caption = "Advanced Search Results - " + totalRecords.ToString() + " record(s) found";
        }
    }

    public void gridAppList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
            AdvSearchCDE adv = new AdvSearchCDE();
            adv.Attributes = ViewState["enabledatt"].ToString();
            adv.ApplicatioName = ViewState["app"].ToString();
            adv.Score = Int32.Parse(ViewState["threshold"].ToString());
            adv.Organization = Int32.Parse(ViewState["organization"].ToString());

            adv.Page = gridAppList.PageIndex;
            adv.SortExpr = ViewState["exprAndDir"].ToString();

            Session["search"] = adv;
            Response.Redirect("~/Application/ApplicationDataEntryView.aspx?id=" +  e.CommandArgument);
        }
    }
}
